package com.easwift.qsp.das.dyy;
import java.io.BufferedInputStream;

import com.easwift.qsp.enums.DeviceTypeEnum;
import com.easwift.qsp.enums.MonitorItemEnum;
import com.easwift.qsp.enums.SensorTypeEnum;
import com.easwift.qsp.util.BitUtils;
import com.easwift.qsp.util.DataSourceUtils;
import com.easwift.qsp.util.FormulaUtils;
import com.easwift.qsp.util.LogUtils;
/**
 * 电压采集仪读数据线程
 * @author Administrator
 *
 */
public class DYYSocketReadThread extends Thread{
	
	private BufferedInputStream bis;
	
	public DYYSocketReadThread(BufferedInputStream bis) {
		super();
		this.bis = bis;
	}
	
	@Override
	public void run(){
		int count = 0;
		try{
			while(true && count < 1){
				if(bis.available() != 0){
					count++;
					byte[] bytes = new byte[bis.available()];
					bis.read(bytes,0,bis.available());
					System.out.println("读取到数据：" + bytes.length + "字节");
					LogUtils.printFrame(bytes);
					//数据长度
					int dataLength = Byte.toUnsignedInt(bytes[12]) + (Byte.toUnsignedInt(bytes[13]) << 8);
					//设备地址
					int address = Byte.toUnsignedInt(bytes[0]) + (Byte.toUnsignedInt(bytes[1]) << 8);
					//电压
					float v = 0;
					byte[] bbs = new byte[4];
					//循环组数
					for(int i = 0;i < 10;i++){
						//循环通道数
						for(int j = 0;j < dataLength / 10;j = j + 4){
							System.arraycopy(bytes,dataLength / 10 * i + j + 16,bbs,0,4);
							bbs = BitUtils.reverseArray(bbs);
							v = BitUtils.byteArrayToFloat(bbs);
							int group = i + 1;
							//不是最后一组时间数据
							if(j + 4 < dataLength / 10){
								int channel = j / 4 + 1;
								System.out.println(String.format("第%s组通道%s电压：%s",group,channel,v));
								String sql = String.format("insert into tb_voltage_data (address,group_num,channel,value,create_time) values (%s,%s,%s,%s,%s)",String.valueOf(address),group,channel,String.valueOf(v),"now()");
								DataSourceUtils.getJdbcTemplate().execute(sql);
								//TODO 根据设备地址和通道查询传感器，查询传感器数据的计算公式和参数值
//								int sensorType = getByAddressAndChannel(address,channel);
//								double[] parameter = getByAddressAndChannel(address,channel);
								int sensorType = SensorTypeEnum.MACHINE_WIND_DIRECTION.getCode();
								double[] parameter = {1,1};
								double value = 0;
								int monitorItem = 0;
								switch(sensorType){
									case 1  : monitorItem = MonitorItemEnum.ACCELERATE.getCode();
									case 4  : if(monitorItem == 0){
												  monitorItem = MonitorItemEnum.EARTHQUAKE.getCode();
											  }
									case 11 : if(monitorItem == 0){
												  monitorItem = MonitorItemEnum.DISTANCE_JI.getCode();
											  }
											  value = FormulaUtils.accelerate(parameter[0],v);
											  break;
									case 2  : value = FormulaUtils.leanAngle(v,parameter[0]);
											  monitorItem = MonitorItemEnum.LEAN_ANGLE.getCode();
											  break;
									case 3  : value = FormulaUtils.windPressure(parameter[0],v,parameter[1]);
											  monitorItem = MonitorItemEnum.WIND_PRESSURE.getCode();
											  break;
									case 5  : value = FormulaUtils.superWindSpeed(v);
											  monitorItem = MonitorItemEnum.SUPER_WIND_SPEED.getCode();
											  break;
									case 6  : value = FormulaUtils.superWindDirection(v);
											  monitorItem = MonitorItemEnum.SUPER_WIND_DIRECTION.getCode();
											  break;
									case 7  : value = FormulaUtils.windAngle(v);
											  monitorItem = MonitorItemEnum.WIND_ANGLE.getCode();
											  break;
									case 8  : value = FormulaUtils.temperature(v);
											  monitorItem = MonitorItemEnum.TEMPERATURE_JI.getCode();
											  break;
									case 9  : value = FormulaUtils.machineWindSpeed(v);
											  monitorItem = MonitorItemEnum.MACHINE_WIND_SPEED.getCode();
											  break;
									case 10 : value = FormulaUtils.machineWindDirection(v);
											  monitorItem = MonitorItemEnum.MACHINE_WIND_DIRECTION.getCode();
											  break;
								}
								System.out.println(String.format("第%s组通道%s%s：%s",group,channel,SensorTypeEnum.getNameByCode(sensorType),value));
								sql = String.format("insert into tb_device_data (address,value,device_type,monitor_item_id,create_time) values (%s,%s,%s,%s,%s)",String.valueOf(address),value,DeviceTypeEnum.EXPLORER.getCode(),monitorItem,"now()");
								DataSourceUtils.getJdbcTemplate().execute(sql);
							}else{
								System.out.println(String.format("第%s组时间：%s",group,v));
							}
						}
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
}